package love.contact.adapter;

import java.util.ArrayList;
import java.util.List;

import wwk.common.util.LayoutUtil;
import love.contact.R;
import love.contact.data.Contact;
import love.contact.data.Group;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class GroupSubObjectListAdapter extends BaseAdapter {
	
	private LayoutInflater mInflater;
	private Group group;
	
	int subGroupCount;
	int subContactCount;
	
	public List<Integer> unexistedContactIdArray=new ArrayList<Integer>();
	
	public GroupSubObjectListAdapter(Context context,Group group)
	{
		mInflater = LayoutInflater.from(context);
		this.group=group;
	}
	
	public void setGroup(Group group)
	{
		this.group=group;
	}

	@Override
	public int getCount() {
		
		subGroupCount=group.subGroupCount();
		subContactCount=group.subContactCount();
		
		return subGroupCount+subContactCount;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) 
	{
		Context context=mInflater.getContext();
		
	 	if (convertView == null) 
        {
            convertView = mInflater.inflate(android.R.layout.simple_list_item_1, null);
	 		//convertView=new TextView(context);
        }
        
        String text=null;
        int textColor=Color.BLACK;
        Drawable drawable=null;
        Drawable drawable_ic_contact=context.getResources().getDrawable(R.drawable.ic_contact_dark);
        
        if(position<subGroupCount)
        {
	        	Group subGroup=group.subGroupAtIndex(position);
	        	if(subGroup!=null)
	        	{
	        		text=subGroup.getName();
	        	}
	        	//textColor=Color.argb(255, 200, 200, 100);
	        	drawable=context.getResources().getDrawable(R.drawable.ic_group_dark);
        }
        else 
        {
	        	int contactId=group.subContactId(mInflater.getContext(),position-subGroupCount);
	        	if(contactId>0)
	        	{
	        		Object[] objs=Contact.getContactNameAndIcon(context,contactId);
	        		if(objs!=null)
	        		{
		        		text=(String)objs[0];
		        		if(text==null)
		        		{
		        			//delete record from relation table if contact not exists
		        			//group.removeSubContact(contactId);
		        		}
		        		
		        		Bitmap bitmap=(Bitmap)objs[1];
		        		if(bitmap!=null)
		        		{
		        			drawable=new BitmapDrawable(context.getResources(),bitmap);
		        		}
	        		}
	        		else
	        		{
	        			unexistedContactIdArray.add(contactId);
	        		}
	        	}
	
	        	if(drawable==null)
	        	{
	        		drawable=drawable_ic_contact;
	        	}
        }
        
        TextView textView=(TextView)convertView;
        textView.setText(text);
        textView.setTextColor(textColor);
        textView.setTextSize(16);
        
        int intrinsicWidth=drawable.getIntrinsicWidth();
        int intrinsicHeight=drawable.getIntrinsicHeight();
        if(drawable!=drawable_ic_contact)
        {
        		intrinsicWidth=Math.min(intrinsicWidth, drawable_ic_contact.getIntrinsicWidth());
            intrinsicHeight=Math.min(intrinsicHeight, drawable_ic_contact.getIntrinsicHeight());
        }
        
        drawable.setBounds(0,0,intrinsicWidth,intrinsicHeight);
        textView.setCompoundDrawables(drawable,null,null,null);
        textView.setCompoundDrawablePadding(10);
        
        //delete unexistedContactIdArray
        if(position>=getCount()-1 && unexistedContactIdArray!=null && unexistedContactIdArray.size()>0)
		{
			for(int contactId : unexistedContactIdArray)
			{
				group.removeSubContact(contactId);
			}
			unexistedContactIdArray.clear();
			notifyDataSetChanged();
		}
        
        return convertView;
	}
	
	public void notifyDataSetChanged() 
	{
		group.openSubCursor();
		
        super.notifyDataSetChanged();
    }
}
